﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using TangoGames.Scene;

namespace incubation_xna3d.Scene
{
    class TurnManager:GameComponent, ITurnManager
    {
        #region Fields

        /// <summary>
        /// Controle de Cena
        /// </summary>
        //private ISceneManager sceneManager;

        /// <summary>
        /// Jogador corrente
        /// </summary>
        private int currentPlayerIndice = 0;

        /// <summary>
        /// Numero do Turno
        /// </summary>
        int turnNumber;

        /// <summary>
        /// Lista de Jogadores
        /// </summary>
        private List<TurnPlayer> playersList;

        /// <summary>
        /// Lista de elementos dos jogadores
        /// </summary>
        private Dictionary<TurnPlayer, List<ITurnElement>> turnElements;

        #endregion

        #region Properties

        /// <summary>
        /// Controle de Cena
        /// </summary>
        //public ISceneManager SceneManager { get { return sceneManager; } set { sceneManager = value; } }

        /// <summary>
        /// Jogador corrente
        /// </summary>
        public TurnPlayer CurrentPlayer { get { return playersList[currentPlayerIndice];} }

        /// <summary>
        /// Numero do Turno
        /// </summary>
        public int TurnNumber { get { return turnNumber; } }

        /// <summary>
        /// Lista de Jogadores
        /// </summary>
        public List<TurnPlayer> PlayersList { get { return playersList; } set { playersList = value; } }

        /// <summary>
        /// Lista de elementos dos jogadores
        /// </summary>
        public Dictionary<TurnPlayer, List<ITurnElement>> TurnElements { get { return  turnElements; } set {  turnElements = value; } }

        /// <summary>
        /// Final dos turnos
        /// </summary>
        public EndAllPlayersTurn HandlerEndAllPlayersTurn { get; set; }

        #endregion

        #region Constructor
        /// <summary>
        /// Construtora
        /// </summary>
        /// <param name="game"></param>
        public TurnManager(Game game, ISceneManager sceneManager)
            : base(game)
        {
            //this.sceneManager = sceneManager;
            turnElements = new Dictionary<TurnPlayer, List<ITurnElement>>();
            playersList = new List<TurnPlayer>();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Final do turn do jogador corrente
        /// </summary>
        public void EndPlayerTurn()
        {
            currentPlayerIndice++;
            if (currentPlayerIndice >= playersList.Count) 
            {
                if (HandlerEndAllPlayersTurn != null) HandlerEndAllPlayersTurn(turnNumber);
                currentPlayerIndice = 0;
                turnNumber++;
            }

            StartPlayerTurn(playersList[currentPlayerIndice]);

        }

        /// <summary>
        /// Inicia o jogo de Turnos
        /// </summary>
        public void StartGame()
        {
            turnNumber = 1;
            currentPlayerIndice = 0;
            StartPlayerTurn(playersList[currentPlayerIndice]);
        }

        private void StartPlayerTurn(TurnPlayer currentPlayer)
        {
            foreach (ITurnElement elem in turnElements[currentPlayer])
            {
                elem.NewTurn(turnNumber);
            }
        }

        #endregion



        #region Update
        #endregion

        #region Draw
        #endregion


    }
}
